import {Injectable} from "@angular/core";
import {BackendService} from "./backend.service";
import {Observable} from "rxjs";
import {Crisis} from "../models/crisis";
import {Logger} from "./logger.service";

@Injectable()
export class CrisisService {
  constructor(private backend: BackendService,
              private logger: Logger) {
  }

  getCrises() {
    return Observable.fromPromise(this.backend.getAll(Crisis)).do((crises)=>
      this.logger.log(`Fetched ${crises.length} crises.`)
    )
  }

  getCrisis(id: number|string) {
    return Observable.fromPromise(this.backend.getAll(Crisis)).map(crises=> {
      return crises.find(crisis => crisis.id === id)
    })
  }
}
